package com.ruoyi.dynamic.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dynamic.domain.DynamicColumn;
import com.ruoyi.dynamic.service.IDynamicColumnService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 代码生成业务字段Controller
 * 
 * @author ningtao
 * @date 2022-11-27
 */
@Controller
@RequestMapping("/dynamic/column")
public class DynamicColumnController extends BaseController
{
    private String prefix = "dynamic/column";

    @Autowired
    private IDynamicColumnService dynamicColumnService;

    @RequiresPermissions("dynamic:column:view")
    @GetMapping()
    public String column()
    {
        return prefix + "/column";
    }

    /**
     * 查询代码生成业务字段列表
     */
    @RequiresPermissions("dynamic:column:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DynamicColumn dynamicColumn)
    {
        startPage();
        List<DynamicColumn> list = dynamicColumnService.selectDynamicColumnList(dynamicColumn);
        return getDataTable(list);
    }

    /**
     * 导出代码生成业务字段列表
     */
    @RequiresPermissions("dynamic:column:export")
    @Log(title = "代码生成业务字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DynamicColumn dynamicColumn)
    {
        List<DynamicColumn> list = dynamicColumnService.selectDynamicColumnList(dynamicColumn);
        ExcelUtil<DynamicColumn> util = new ExcelUtil<DynamicColumn>(DynamicColumn.class);
        return util.exportExcel(list, "代码生成业务字段数据");
    }

    /**
     * 新增代码生成业务字段
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存代码生成业务字段
     */
    @RequiresPermissions("dynamic:column:add")
    @Log(title = "代码生成业务字段", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DynamicColumn dynamicColumn)
    {
        return toAjax(dynamicColumnService.insertDynamicColumn(dynamicColumn));
    }

    /**
     * 修改代码生成业务字段
     */
    @RequiresPermissions("dynamic:column:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DynamicColumn dynamicColumn = dynamicColumnService.selectDynamicColumnById(id);
        mmap.put("dynamicColumn", dynamicColumn);
        return prefix + "/edit";
    }

    /**
     * 修改保存代码生成业务字段
     */
    @RequiresPermissions("dynamic:column:edit")
    @Log(title = "代码生成业务字段", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DynamicColumn dynamicColumn)
    {
        return toAjax(dynamicColumnService.updateDynamicColumn(dynamicColumn));
    }

    /**
     * 删除代码生成业务字段
     */
    @RequiresPermissions("dynamic:column:remove")
    @Log(title = "代码生成业务字段", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dynamicColumnService.deleteDynamicColumnByIds(ids));
    }
}
